<template>
  <!-- <el-button text @click="dialogFormVisible = true"
    >open a Form nested Dialog</el-button
  > -->

  <el-dialog v-model="dialogFormVisible" title="添加用户">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="formLabelWidth">
        <el-input v-model="form.password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" :label-width="formLabelWidth">
        <el-input v-model="form.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="电话" :label-width="formLabelWidth">
        <el-input v-model="form.mobile" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, defineEmits } from "vue";
import { addUsers } from "@/api/Users";
import { ElMessage } from "element-plus";
// 调用父组件方法并传入表单数据
const emit = defineEmits(["getFromDate"]);

const dialogTableVisible = ref(false);
// 打开弹窗
const dialogFormVisible = ref<boolean>(false);
const formLabelWidth = "140px";
const openDialog = () => {
  dialogFormVisible.value = true;
};
// 将子组件的方法暴露出去
defineExpose({
  openDialog,
});
const form = reactive({
  username: "",
  password: "",
  email: "",
  mobile: "",
});

// 添加用户信息
const addUser = async () => {
  if (
    form.username == "" ||
    form.password == "" ||
    form.email == "" ||
    form.mobile == ""
  ) {
    ElMessage.error("不能存在空值");
    return false;
  }
  let res = await addUsers(form);
  //   {
  //   username: "知恩测试",
  //   password: "123456",
  //   email: "123222@qq.com",
  //   mobile: "15019877777",
  // }
  console.log(res, res.data.meta.msg);
  dialogFormVisible.value = false;
  emit("getFromDate", res.data.data);
  // form.username = "";
  // form.password = "";
  // form.email = "";
  // form.mobile = "";
};
</script>
<style scoped>
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
